package org.wishwork.api.inventory.upload.request;

import cn.hutool.core.util.StrUtil;
import lombok.*;
import org.wishwork.exception.ExceptionEnum;
import org.wishwork.exception.JSTException;

@EqualsAndHashCode
@Getter
@ToString
public class Item {

    /**
     * 必填
     * 商品编码
     */
    private String skuId;

    /**
     * 必填
     * 数量
     */
    private Integer qty;

    /**
     * 非必填
     * 批次号;批次号存在批次信息和有效期必填
     */
    private String batchId;

    /**
     * 非必填
     * 批次日期
     */
    private String producedDate;

    /**
     * 非必填
     * 有效期
     */
    private String expirationDate;


    private Item(String skuId, Integer qty, String batchId, String producedDate, String expirationDate) {
        this.skuId = skuId;
        this.qty = qty;
        this.batchId = batchId;
        this.producedDate = producedDate;
        this.expirationDate = expirationDate;
    }

    public static Builder builder() {
        return new Builder();
    }


    public static class Builder {
        private String skuId;
        private Integer qty;
        private String batchId;
        private String producedDate;
        private String expirationDate;

        public Builder skuId(String skuId) {
            if (StrUtil.isEmpty(skuId)) {
                throw new JSTException(ExceptionEnum.USER_ERROR_A0410, "skuId must be not null or blank");
            }
            this.skuId = skuId;
            return this;
        }

        public Builder qty(Integer qty) {
            if (qty == null) {
                throw new JSTException(ExceptionEnum.USER_ERROR_A0410, "qty must be not null or blank");
            }
            this.qty = qty;
            return this;
        }

        public Builder batchId(String batchId) {
            this.batchId = batchId;
            return this;
        }

        public Builder producedDate(String producedDate) {
            this.producedDate = producedDate;
            return this;
        }

        public Builder expirationDate(String expirationDate) {
            this.expirationDate = expirationDate;
            return this;
        }

        public Item build() {
            if (StrUtil.isEmpty(this.skuId)) {
                throw new JSTException(ExceptionEnum.USER_ERROR_A0410, "skuId not set");
            }
            if (this.qty == null) {
                throw new JSTException(ExceptionEnum.USER_ERROR_A0410, "qty not set");
            }
            if (StrUtil.isNotEmpty(this.batchId)) {
                if (StrUtil.isEmpty(this.producedDate)) {
                    throw new JSTException(ExceptionEnum.USER_ERROR_A0410, "producedDate not set");
                }
                if (StrUtil.isEmpty(this.expirationDate)) {
                    throw new JSTException(ExceptionEnum.USER_ERROR_A0410, "expirationDate not set");
                }
            }
            return new Item(this.skuId, this.qty, this.batchId, this.producedDate, this.expirationDate);
        }
    }
}